<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="x" uri="http://www.yf1706.com/yf1706/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table id="role-list-dg" class="easyui-datagrid" 
	        data-options="url:'../grant/getAllRoles',fit:true,fitColumns:true,singleSelect:true,method:'get',border:false,
	        	toolbar:'#role-list-dg-tb'">   
	    <thead>   
	        <tr>   
	            <th data-options="field:'roleId',width:80">编号</th>   
	            <th data-options="field:'roleName',width:100">名称</th>   
	            <th data-options="field:'xxx',width:400,formatter: roleFormatter">操作</th>   
	        </tr>   
	    </thead>   
	</table>  
	<div id="role-list-dg-tb">
		<x:g id="5">
		<a href="javascript:void(0)" id="role-list-dg-tb-add" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true,text:'添加角色'"></a>
		</x:g>
	</div>
	
	<!-- 编辑角色的对话框 -->
	<div id="role-edit-dlg" class="easyui-dialog" title="编辑角色" style="width:400px;height:200px;"   
	        data-options="iconCls:'icon-save',resizable:true,modal:true,bodyCls:'bodyCls',closed:true,
	        buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					$('#role-edit-dlg-form').form({
						onSubmit:function(params){
							return $('#role-eidt-dlg-form').form('validate');
						},
						success:function(v){
							v = eval('(' + v + ')');
							if(v.success){
								$('#role-edit-dlg').dialog('close');
								$('#role-list-dg').datagrid('reload');
								$('#role-eidt-dlg-form').form('reset');
							}else{
								$.messager.alert('错误', v.message);
							}
						}
					}).submit();
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#role-edit-dlg').dialog('close');
				}
			}]">
	        <form id="role-edit-dlg-form" method="post" action="../grant/updateRole">
	        	<input id="roleId" name="roleId" type="hidden"/>
		        <div class="line">
					<input class="easyui-textbox" id="roleName" name="roleName" data-options="label:'角色名',required:true, width:'90%'">
				</div>
			</form>
	</div>  
		
	<!-- 添加新角色的对话框 -->
	<div id="role-add-dlg" class="easyui-dialog" title="增加新角色" style="width:400px;height:200px;"   
	        data-options="iconCls:'icon-save',resizable:true,modal:true,bodyCls:'bodyCls',closed:true,
	        buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					$('#role-add-dlg-form').form({
						onSubmit:function(params){
							return $('#role-add-dlg-form').form('validate');
						},
						success:function(v){
							v = eval('(' + v + ')');
							if(v.success){
								$('#role-add-dlg').dialog('close');
								$('#role-list-dg').datagrid('reload');
								$('#role-add-dlg-form').form('reset');
							}else{
								$.messager.alert('错误', v.message);
							}
						}
					}).submit();
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#role-add-dlg').dialog('close');
				}
			}]">
	        <form id="role-add-dlg-form" method="post" action="../grant/addNewRole">
		        <div class="line">
					<input class="easyui-textbox" id="roleName" name="roleName" data-options="label:'角色名',required:true, width:'90%'">
				</div>
			</form>
	</div>  
	
	<!-- 给角色授权的对话框 -->
	<div id="role-grant-dlg" class="easyui-dialog" title="给角色授权" 
        data-options="iconCls:'icon-cut',closed:true,resizable:true,modal:true,width:500,height:500,buttons:[{
				text:'授权',
				iconCls:'icon-ok',
				handler:function(){
					$('<form id=role-grant-dlg-form method=post>').form({
						url:'../grant/grantRole',
						onSubmit: function(params){
							//角色id
							var roleId = $('#role-grant-dlg').dialog('options').roleId;
							params.roleId = roleId;
							//功能id
							var nodes = $('#role-grant-dlg-tree').tree('getChecked');
							var j = 0;
							for(var i = 0; i < nodes.length; i ++){
								if(!nodes[i].children){
									params['funcIds[' + j + ']'] = nodes[i].id;
									j ++;
								}
							}
						},
						success:function(v){
							var obj = eval('(' + v + ')');
							if(obj.success){
								$('#role-grant-dlg').dialog('close');
							}else{
								$.messager.alert('错误', '操作失败，原因：' + obj.message);
							}
						}
					}).submit().appendTo($('#role-extra-label'));
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#role-grant-dlg').dialog('close');
				}
			}]">   
        <ul id="role-grant-dlg-tree" class="easyui-tree" data-options="fit:true,checkbox:true"></ul>  
	</div>

	<div id="role-extra-label"></div>
	
	<div id="user_role_list_win">
		<input type="hidden" id="user_role_id"/>
		<table id="user_role_list_dg"></table>
	</div>
	<div id="user_role_list_dg_tb">
		<a href="javascript:void(0)" id="user_role_list_dg_tb_save" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true,text:'保存'"></a>
		<a href="javascript:void(0)" id="user_role_list_dg_tb_cancel" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true,text:'取消'"></a>
	</div>
	
	<script type="text/javascript">
		var roleFormatter = function(value,row,index){
			console.log(row);
			var edit = "", del = "", grant = "", addUser = "";	
			edit = " <a style='margin:3px 0px;' btn='cell_btn_edit' onclick='editRole(" + row.roleId + ")'>编辑</a>";	
			del = " <a btn='cell_btn_del' onclick='delRole(" + row.roleId + ")'>删除</a>";
				grant = " <a btn='cell_btn_grant' onclick='grantRole(" + row.roleId + ")'>给角色授权</a>";
				addUser  = " <a btn='cell_btn_adduser' onclick='addUser(" + row.roleId + ")'>编辑用户</a>";
			return edit + del + grant + addUser;
		}
		
		//删除角色
		var delRole = function(roleId){
			$.messager.confirm('确认', '您确定要删除该角色吗？', function(f){
				if(f){
				$.post("../grant/deleteRole", {roleId: roleId}, function(v){
					if(v.success){
						$('#role-list-dg').datagrid('reload');
					}else{
						$.messager.alert('错误', v.message);
					}
				});
				}
			 });	
		};
		
		//编辑角色
		var editRole = function(roleId){
			$("#roleId").val(roleId);
			$("#role-edit-dlg").dialog("open");	
		};
		
		
		//打开给角色授权的对话框
		var grantRole = function(roleId){
			$("#role-grant-dlg").dialog({roleId: roleId, onOpen:function(){
				$("#role-grant-dlg-tree").tree({method:'get', url: '../grant/getFunctionTypeExs?roleId=' + roleId});
			}});
			
			$("#role-grant-dlg").dialog("open");
		}
		
		//编辑用户
		var addUser = function(roleId){
			$("#user_role_id").val(roleId);
			$("#user_role_list_dg").datagrid('load','../grant/findUserEmpRoleVO?roleId='+roleId);
			$("#user_role_list_win").window('open');
		}
		
		$(function(){
			$("#role-list-dg").datagrid({onLoadSuccess: function(){
				$("a[btn=cell_btn_edit]").linkbutton({iconCls:'icon-edit', plain:true});
				$("a[btn=cell_btn_del]").linkbutton({iconCls:'icon-cancel', plain:true});
				$("a[btn=cell_btn_grant]").linkbutton({iconCls:'icon-reload', plain:true});
				$("a[btn=cell_btn_adduser]").linkbutton({iconCls:'icon-add', plain:true});
			}});
			
			$("#role-list-dg-tb-add").linkbutton({onClick:function(){
				$("#role-add-dlg").dialog('open');
			}});
			
			$("#user_role_list_win").window({
				width:400,
			    height:400,
			    modal:true,
			    minimizable:false,
			    maximizable:false,
			    collapsible:false
			});
			$("#user_role_list_win").window('close');
			$("#user_role_list_dg").datagrid({
			    modal:true,
			    toolbar:"#user_role_list_dg_tb",
			    fit:true,
			    fitColumn:true,
				onLoadSuccess: function (data) {
		            if (data) {
		                $.each(data.rows, function (index, item) {
		                    if (item.ifSelect) {
		                        $('#user_role_list_dg').datagrid('checkRow', index);
		                    }
		                });
		            }	
		       	},
		       	columns:[[
			    	{field:'',title:'',checkbox:true},
					{field:'userId',title:'用户编号',width:'50%'},
					{field:'empName',title:'用户名称',width:'50%'},
			    ]],
			});
			$("#user_role_list_dg_tb_save").linkbutton({onClick:function(){
				var roleId=$("#user_role_id").val();
				var rows=$("#user_role_list_dg").datagrid('getSelections');
				var json={};
				json.roleId=roleId;
				json.userIds=[];
				for(var i=0;i<rows.length;i++){
					json.userIds[i]=(rows[i].userId);
				}
				console.log(json);
				jsonStr=JSON.stringify(json);
				$.ajax({
					url:'../grant/addUserForRole',
					data:{"jsonStr":jsonStr},
					success:function(data){
						console.log(data);
						$.messager.alert('',data.message);
					},
					error:function(){
						$.messager.alert('','操作失败!');
					}
				});
				$("#user_role_list_win").window('close');
			}});
			$("#user_role_list_dg_tb_cancel").linkbutton({onClick:function(){
				$("#user_role_list_win").window('close');
			}});
		});
	</script>
	
</body>
</html>